package _15_XML与JSON._2_JSON;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.jupiter.api.Test;

public class FastJSONTest {

    /**
     * fastjson 主要的三个对象
     * JSON
     *     作用：转换
     *          将Java对象序列号为JSON字符串
     *          将JSON字符串反序列化为Java对象
     *      常用方法：
     *          //JSON字符串反序列化
     *
     *              //转JavaBean
     *              JSON.parseObject(String text, Class<T> clazz)
     *
     *              //转JSONObject
     *              JSON.parseObject(String text)
     *              JSONObject.parseObject(String text)
     *
     *          //数组转JSONArray
     *              parseArray(String text, Class<T> clazz)
     *          //
     *              JSON.toJSONString(Object object)
     *
     * JSONObject
     *      说明：数据以Key-value的形式保存（实现Java的Map接口）
     *      常用方法：
     *          getString(String key)
     *          remove(Object key)
     *
     * JSONArray
     *      说明：是JSON数组，存储的是JSON对象（实现List接口）
     *      常用方法：
     *          getJSONObject(int index)
     *
     *  https://zhuanlan.zhihu.com/p/97222262
     */

    @Test
    public void fastJSONTest(){
        JSONStringVO jsonStringVO = new JSONStringVO();
        String s = jsonStringVO.jsonString;
        JSONObject jsonObject = JSONObject.parseObject(s);
        System.out.println();
        JSONObject jsonObject1 = JSON.parseObject(s);
        System.out.println();

        //获取对象
//        JSONObject carQuoteQuotationDateJSONObject = (JSONObject) jsonObject.get("carQuoteQuotationDate");
        JSONObject carQuoteQuotationDateJSONObject = jsonObject.getJSONObject("carQuoteQuotationDate");
        JSONObject prpCmainBIJSONObject = (JSONObject) carQuoteQuotationDateJSONObject.get("prpCmainBI");
        //获取对象中的值
        if(prpCmainBIJSONObject.containsKey("agentCode")) {
            String agentCode = (String) prpCmainBIJSONObject.get("agentCode");
            System.out.println(agentCode);
        }
        //获取数组或list
        JSONArray carQuoteTransProposalReqListJSONArray = jsonObject.getJSONArray("carQuoteTransProposalReqList");
        //从数组中获取值
        System.out.println("carQuoteTransProposalReqListJSONArray: " + carQuoteTransProposalReqListJSONArray.size());
        JSONObject carQuoteTransProposalReqListJSONObject = carQuoteTransProposalReqListJSONArray.getJSONObject(0);
        if(carQuoteTransProposalReqListJSONObject.containsKey("resource")){
            String aliasName = (String) carQuoteTransProposalReqListJSONObject.get("resource");
            System.out.println("resource: " + aliasName);
        }


        JSONArray carQuoteEngageaReqListJSONArray = carQuoteTransProposalReqListJSONObject.getJSONArray("carQuoteEngageaReqList");
        System.out.println("carQuoteEngageaReqListJSONArray.size(): " + carQuoteEngageaReqListJSONArray.size());

        for(int i = 0; i < carQuoteEngageaReqListJSONArray.size(); i++){
            JSONObject carQuoteEngageaReqListJSONObject = carQuoteEngageaReqListJSONArray.getJSONObject(i);
            System.out.println("i = " + (i + 1));
            System.out.println("clauseCode: " + carQuoteEngageaReqListJSONObject.get("clauseCode"));
            System.out.println("clauseName: " + carQuoteEngageaReqListJSONObject.get("clauseName"));
            System.out.println("clauses: " + carQuoteEngageaReqListJSONObject.get("clauses"));
            System.out.println("itemKindNo: " + carQuoteEngageaReqListJSONObject.get("itemKindNo"));
        }

        //从数据中获取数组
        System.out.println();

    }

    @Test
    public void f(){
        JSONStringVO jsonStringVO = new JSONStringVO();
        String s = jsonStringVO.jsonString4;
        JSONObject jsonObject = JSONObject.parseObject(s);
        System.out.println(jsonObject.toString());
        jsonObject.put("name","jby");
        System.out.println(jsonObject.toString());

    }
}
